package com.hs.homework_02.controller;

import com.hs.homework_02.entitiy.TbStudent;
import com.hs.homework_02.service.TbStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName StudentController
 * @Description TODO
 * @Author 郭敖
 * @Date 2023年08月15日 20:11
 * @Version 1.0
 */
@Api(tags = "学生数据接口",value = "学生数据接口")
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private TbStudentService service;

    @ApiOperation(tags = "修改学生的数据",value = "更新学生的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student",value = "封装了要更新的学生的信息")
    })
    @PutMapping
    public String update(@RequestBody TbStudent student){
        try {
            service.updateById(student);
        }catch (RuntimeException e){
            e.printStackTrace();
            return e.getMessage();
        }
        return "更新成功!!";
    }

    @ApiOperation(tags = "新增学生", value = "新增学生")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "student", value = "封装了要新增的学生的信息")
    })
    @PostMapping
    public String addStudent(@RequestBody TbStudent student) {
        try{
            service.save(student);
        }catch (RuntimeException e){
            return e.getMessage();
        }
        return "success";
    }
}
